package org.lwj.logger.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;

public class Util {

	public static final SimpleDateFormat shortFormat = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat longFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String logId() {
		String logid = UUID.randomUUID().toString().replaceAll("-", "");
		return logid;
	}

	public static boolean isEmpty(String s) {
		if (s == null || s.length() == 0) {
			return true;
		}
		return false;
	}

	public static boolean isEmpty(Object[] array) {
		if (array == null || array.length == 0) {
			return true;
		}
		return false;
	}

	public static boolean isEmpty(Collection<?> array) {
		if (array == null || array.size() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 字符串删除空格
	 * 
	 * @param s
	 * @return
	 */
	public static String delBlank(String s) {
		if (isEmpty(s)) {
			return s;
		}
		return s.replaceAll(Constant.BLANK_STRING, Constant.EMPTY_STRING);
	}

	/**
	 * 字符串数组拼接成字符串
	 * 
	 * @param array     字符串数组
	 * @param separator 分隔符
	 * @return
	 */
	public static String arrayToString(String[] array, String separator) {
		StringBuilder sb = new StringBuilder();
		if (!isEmpty(array)) {
			sb.append(array[0]);
			for (int i = 1; i < array.length; ++i) {
				sb.append(separator).append(array);
			}
		}
		return sb.toString();
	}

	public static Date dateParse(DateFormat dateFormat, String date) throws ParseException {
		return dateFormat.parse(date);
	}

	public static String dateFormat(DateFormat dateFormat, Date date) {
		return dateFormat.format(date);
	}

	public static Date shortParse(String date) throws ParseException {
		return dateParse(shortFormat, date);
	}

	public static String shortFormat(Date date) {
		return dateFormat(shortFormat, date);
	}

	public static Date longParse(String date) throws ParseException {
		return dateParse(longFormat, date);
	}

	public static String longFormat(Date date) {
		return dateFormat(longFormat, date);
	}

	/**
	 * 判断一个类是否已经被加载 (只加载，不链接)
	 * 
	 * @param className
	 * @return
	 */
	public static boolean isClassLoaded(String className) {
		try {
			Util.class.getClassLoader().loadClass(className);
			return true;
		} catch (ClassNotFoundException e) {
			return false;
		}
	}

}
